<template>
	<view class="web">
		<web-view :src="url"></web-view>
	</view>
</template>

<script>
	export default {
		name: "web",
		data() {
			return {
				url: '',
				webviewStyles: {
					progress: {
						width: '100%',
						height: '100%',
					}
				}
			}
		},
		onLoad(option) {
			// console.log(option)
			this.getUrl(unescape(option.url));
		},
		onShareAppMessage() {
			let args = {
				path: '/pages/web/web',
				params: {
					url: encodeURIComponent(this.url)
				}
			};
			return this.$shareAppMessage(args);
		},
		methods: {
			getUrl(url) {
				url = decodeURIComponent(url);
				let arr = url.split('?');
				let uri = arr[0];
				let param = arr[1] ? arr[1].split('&') : '';
				if (param) {
					url = uri;
					if (this.$user.isLogin()) {
						for (let i in param) {
							if (param[i] === 'visiter_id=') {
								let access_token = this.$store.state.user.accessToken;
								param[i] += access_token || '';
							}
							let userInfo = this.$store.state.user.info;
							if (param[i] === 'visiter_name=') {
								param[i] += encodeURIComponent(userInfo ? userInfo.nickname : '游客');
							}
							if (param[i] === 'avatar=') {
								param[i] += encodeURIComponent(userInfo ? userInfo.avatar : '');
							}
						}
					}
					let pages = getCurrentPages();
					// console.log(pages);
					// console.log(pages[pages.length-2].data);
					if (pages.length > 1) {
						let page = pages[pages.length - 2];
						if (page.data.goods) {
							let goods = '';
							let product = '';
							goods = page.data.goods;
							product = {
								pid: goods.id,
								title: goods.name,
								img: goods.cover_pic,
								info: '',
								price: "￥" + goods.price,
								good_price: "￥" + goods.original_price,
								url: ""
							};
							if (page.route === 'plugins/pt/goods/goods') {
								let detgoods = page.data.detail
								product.pid = page.data.goods_id
								product.title = detgoods.name,
									product.img = detgoods.cover_pic,
									product.price = "￥" + detgoods.attr[0].price;
								product.good_price = "￥" + detgoods.attr[0].original_price;
								product.pricea = "￥" + detgoods.attr[0].price;
								product.good_pricea = "￥" + detgoods.attr[0].original_price;
								product.priceb = "￥" + detgoods.attr[1].price;
								product.good_priceb = "￥" + detgoods.attr[1].original_price;
								product.priceab = "￥" + detgoods.attr[2].price;
								product.good_priceab = "￥" + detgoods.attr[2].original_price;
							}
							if (page.route === 'plugins/integral_mall/goods/goods') {
								product.price = '';
								if (typeof goods.integral != 'undefined') {
									product.price += goods.integral + "金币 + ";
								}
								product.price += "￥" + goods.price;
								product.good_price += "￥" + goods.original_price;
							}
							if (page.route === 'plugins/step/goods/goods') {
								product.price = goods.price;
								product.good_price += "￥" + goods.original_price;
							}
							if (product) {
								param.push("product=" + encodeURIComponent(JSON.stringify(product)));
							}
						}
					}
					url = uri + '?' + param.join('&');
				}
				this.url = url
				
				// uni.navigateBack()
			}
		}
	}
</script>

<style scoped>
	.web {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}
</style>
